package com.cjbdi.dao;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.cjbdi.entity.UserInfo;

import java.util.List;

@Repository
public interface UserRepository extends JpaRepository<UserInfo, Long> {

	@Query(value = "select * from t_user as a where id = :id", nativeQuery = true)
    List<UserInfo> findById(int id);
    
    @Query(value = "select * from t_user as a where a.name = ?1 and password = ?2", nativeQuery = true)
    List<UserInfo> findByNameAndPassword(String name,String password);
    
    @Transactional
    @Modifying(clearAutomatically = true)
    @Query(value = "delete from t_user where id=:id",nativeQuery = true)//原生SQL
    Integer delete(@Param("id")int id);
    
    @Query(value = "select * from t_user where case when :name!=''then name like '%'||:name||'%' else 1=1 end and case when :fymc!='' then fymc like '%'||:fymc||'%' else 1=1 end and case when :province!=''then province=:province else 1=1 end and case when :fytype!='' then fytype=:fytype else 1=1 end" , nativeQuery = true)
    Page<UserInfo> getPageByUserInfo(@Param("name") String name,@Param("fymc") String fymc,@Param("province") String province,@Param("fytype") String fytype,Pageable pageable);

    @Query(value = "select * from t_user where userip = ?1", nativeQuery = true)
    List<UserInfo> findByIp(String ip);
    
    @Query(value = "select * from t_user where fybm in(:fybm)", nativeQuery = true)
    List<UserInfo> findByFybm(List<String> fybm);
}
